// pages/Bank-card-management2/Bank-card-management2.js
const SERVICE = require("../../service.js");
// import '../../public/js/yinhangkayanzhen.js';
let ROTJS = require("../../public/js/yinhangkayanzhen.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    banlCardName:"",
    show:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let _this = this;
    let openid = wx.getStorageSync("token");    
    wx.showNavigationBarLoading();
    SERVICE.getBankCardName({openid},(status,data)=>{
      console.log(data);
      wx.hideNavigationBarLoading();
      _this.setData({
        bankCartList:data.data
      });
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  add(){
    this.setData({
      show:!this.data.show
    });
  },
  input(e){
    let value = e.detail.value;
    if (value.length > 0 && ROTJS.CheckBankNo(value) !== true){
      wx.showToast({
        title: "银行卡号不正确",
        icon: "none",
        mask: true
      });
    }
  },
  list(e){
    let id = e.currentTarget.dataset.id,
      name = e.currentTarget.dataset.name,
      item = e.currentTarget.dataset.index;
      console.log(id);
      this.setData({
        banlCardName: name,
        active: item,
        show:false,
        id
      });
  },
  formSubmit(e){
    let td = this.data,
      name = e.detail.value.name,
      card_number = e.detail.value.cardnumber,
      bank_card = e.detail.value.bankcard;
    let openid = wx.getStorageSync("token");      
    if (name.length == 0){
      wx.showToast({
        title: "请输入姓名",
        icon:"none",
        mask:true
      });
    } else if (card_number.length == 0){
      wx.showToast({
        title: "请输入卡号",
        icon: "none",
        mask: true
      });
    } else if (card_number.length > 0 && ROTJS.CheckBankNo(card_number) !== true){
      wx.showToast({
        title: "银行卡号不正确",
        icon: "none",
        mask: true
      });
    } else if (bank_card.length == 0){
      wx.showToast({
        title: "请选择银行卡",
        icon: "none",
        mask: true
      });
    }else{
      SERVICE.addBankCard({
        user_name: name,
        card_number: card_number,
        bank_id:td.id,
        openid
      },(status,data)=>{
        wx.showToast({
          title: data.message,
          icon:"none",
          mask:true
        });
        if(status){
          wx.navigateBack({});
        }
      });
    }
  }
})